package com.gome.axwl.aoc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gome.axwl.aoc.common.util.BeanJsonUtil;
import com.gome.axwl.aoc.common.util.DateUtil;
import com.gome.axwl.aoc.dao.DriverIntegrationDao;
import com.gome.axwl.aoc.entity.driver.DriverIntegrationParamForOcsDto;
import com.gome.axwl.aoc.entity.dto.DriverEvaluationDataFromOrderDto;
import com.gome.axwl.aoc.entity.dto.DriverIntegrationDto;
import com.gome.axwl.aoc.entity.dto.DriverIntegrationParamDto;
import com.gome.axwl.aoc.entity.dto.PageResultDto;
import com.gome.axwl.aoc.entity.po.DriverIntegrationParamPo;
import com.gome.axwl.aoc.entity.po.DriverIntegrationPo;
import com.gome.axwl.aoc.service.DriverIntegrationService;
import com.gome.axwl.aoc.service.feign.OrderFeignClient;
import com.gome.commons.core.ErrorCode;
import com.gome.commons.core.ResultData;
import com.gome.datasource.shard.annotation.ReadDatasource;
import com.gome.datasource.shard.annotation.WriteDatasource;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description: 司机积分业务service实现类
 * @Auther: yinyuxin@gome.com.cn
 * @Date: 2018/4/28 11:29
 */
@Service("driverIntegrationService")
public class DriverIntegrationServiceImpl implements DriverIntegrationService {

    private static final Logger LOGGER=LoggerFactory.getLogger(GradeRuleServiceImpl.class);


    @Autowired
    private DriverIntegrationDao driverIntegrationDao;

    @Autowired
    private OrderFeignClient orderFeignClient;


    @Override
    @ReadDatasource
    public PageInfo<DriverIntegrationPo> queryAllDriverIntegration(DriverIntegrationParamPo driverIntegrationParamPo,Integer pageNum,Integer pageSize) {

        if (null==driverIntegrationParamPo.getIntegrationDate()||"".equals(driverIntegrationParamPo.getIntegrationDate())){
            driverIntegrationParamPo.setIntegrationDate(DateUtil.date2String(new Date(),DateUtil.FORMAT_DATE_WITHOUT_DAY));
        }
        PageHelper.startPage(pageNum==null?1:pageNum,pageSize==null?10:pageSize);
        List<DriverIntegrationPo> driverIntegrationPos = driverIntegrationDao.queryList(driverIntegrationParamPo);
        PageInfo<DriverIntegrationPo> pageInfo=new PageInfo<>(driverIntegrationPos);
        return pageInfo;

    }

    @Override
    @ReadDatasource
    public PageInfo<DriverIntegrationPo> queryDriverIntegrationList(String carrierNo, String driverCode,String orderField,Integer orderSort,Integer pageIndex,Integer pageSize) {
        DriverIntegrationParamPo driverIntegrationParamPo=new DriverIntegrationParamPo();
        //初始化默认查询参数
        if (null==orderField || "".equals(orderField)){
            orderField="date";
        }
        driverIntegrationParamPo.setProviderCode(carrierNo);
        driverIntegrationParamPo.setDriverCode(driverCode);
        driverIntegrationParamPo.setOrderField(orderField);
        driverIntegrationParamPo.setOrderSort(orderSort);
        PageHelper.startPage(null==pageIndex?1:pageIndex,null==pageSize?10:pageSize);
        List<DriverIntegrationPo> driverIntegrationPos = driverIntegrationDao.queryList(driverIntegrationParamPo);
        PageInfo<DriverIntegrationPo> pageInfo=new PageInfo<>(driverIntegrationPos);
        return pageInfo;
    }

    @Override
    @ReadDatasource
    public DriverIntegrationPo queryDriverIntegrationByMonth(String driverCode, String providerCode,String dateString) {
        DriverIntegrationParamPo driverIntegrationParamPo=new DriverIntegrationParamPo();
        driverIntegrationParamPo.setDriverCode(driverCode);
        driverIntegrationParamPo.setIntegrationDate(dateString);
        driverIntegrationParamPo.setProviderCode(providerCode);
        List<DriverIntegrationPo> driverIntegrationPos = driverIntegrationDao.queryList(driverIntegrationParamPo);
        if (CollectionUtils.isEmpty(driverIntegrationPos)){
            return null;
        }
        return driverIntegrationPos.get(0);
    }

    @Override
    @WriteDatasource
    public void batchInsert(List<DriverIntegrationPo> driverIntegrationPos) {
        driverIntegrationDao.batchInsert(driverIntegrationPos);
    }

    @Override
    public ResultData queryDriverTakingsDetail(Integer driverId, String month) {
        return orderFeignClient.queryDriverTakingsDetail(driverId,month);
    }

    @Override
    public ResultData<DriverEvaluationDataFromOrderDto> queryDriverCount(Integer driverId, String month) {
        return orderFeignClient.queryDriverCount(driverId,month);
    }

    @Override
    public ResultData<List<DriverEvaluationDataFromOrderDto>> queryDriverCounts(DriverIntegrationParamForOcsDto driverIntegrationParamForOcsDto) {
        return orderFeignClient.queryDriverCounts(driverIntegrationParamForOcsDto);
    }

    @Override
    @WriteDatasource
    public void batchUpdate(List<DriverIntegrationPo> driverIntegrationPos) {
        driverIntegrationDao.batchUpdate(driverIntegrationPos);
    }


}
